//
//  FLLRelatedViewController.m
//  FLLGodOfCooking
//
//  Created by LL.F on 16/9/13.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLRelatedViewController.h"
#import "FLLMacro.h"
#import "FLLRelatedModel.h"
#import "FLLFoodMaterialTableViewCell.h"
#import "FLLFoodMatreialNameTableViewCell.h"
#import "FLLRelatedTableViewCell.h"
#import "RelatedData.h"

@interface FLLRelatedViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *relatedTableView;
@property (nonatomic, strong) FLLRelatedModel *fllRelatedModel;
@end

@implementation FLLRelatedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createPOSTRequest];
    [self createRelatedTableView];
    
    
}
- (void)createPOSTRequest
{
    
    
    NSString *bodyString = [NSString stringWithFormat:@"methodName=DishesCommensense&dishes_id=%@&version=4.40&", _idString];
    [ZWYNetTool POST:@"http://api.izhangchu.com/ HTTP/1.1" andBody:bodyString andBodyStyle:ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded; charset=utf-8"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        
        self.fllRelatedModel = [FLLRelatedModel modelWithDic:result];
      
        [self.relatedTableView reloadData];
        
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
}
- (void)createRelatedTableView
{
    
    self.relatedTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 95 * kHeightScale) style:UITableViewStylePlain];
    self.relatedTableView.delegate = self;
    self.relatedTableView.dataSource = self;
    self.relatedTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.relatedTableView.bounces = NO;
    [self.relatedTableView registerNib:[UINib nibWithNibName:@"FLLFoodMaterialTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"foodMaterialTableCell"];
    [self.relatedTableView registerNib:[UINib nibWithNibName:@"FLLRelatedTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"relatedTableCell"];
    [self.view addSubview:_relatedTableView];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 0) {
        FLLFoodMaterialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"foodMaterialTableCell"];
        [cell.materialImage sd_setImageWithURL:[NSURL URLWithString:self.fllRelatedModel.data.image] placeholderImage:[UIImage new] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        cell.blackImage.image = [UIImage new];
    return cell;
    }
    FLLRelatedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"relatedTableCell"];
    cell.grayLabel.backgroundColor = FLLLightGrayColor;
    cell.titleLabel.font = [UIFont systemFontOfSize:15 *kWidthScale];
    cell.contentLabel.font = [UIFont systemFontOfSize:13 *kWidthScale];
    if (indexPath.section == 1) {
        cell.titleLabel.text = @"相关常识";
        
        cell.contentLabel.text = self.fllRelatedModel.data.nutrition_analysis;
        
    } if (indexPath.section == 2) {
        cell.titleLabel.text = @"制作指导";
        cell.contentLabel.text = self.fllRelatedModel.data.production_direction;
      

    }
    return cell;
    

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    if (indexPath.section == 0) {
        return SCREEN_WIDTH * 2 / 3.0;
    } if (indexPath.section == 1) {
        return SCREEN_WIDTH / 9.0 + [GetHeightTools heightWith:self.fllRelatedModel.data.nutrition_analysis] + 10;// 10 是grayLabel的高度
    }
    return SCREEN_WIDTH / 9.0 + [GetHeightTools heightWith:self.fllRelatedModel.data.production_direction] + 10;
    
}
- (void)setIdString:(NSString *)idString
{

    _idString = idString;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
